package com.tianyi.wz.walkinggame.common;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import org.apache.commons.lang3.StringUtils;

public class AlertCommon {

    Activity activity;

    public AlertCommon(Activity activity){
        this.setActivity(activity);
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    /**
     * 弹出提示
     * @param title 标题
     * @param mesage 信息
     * @param positiveText 肯定按钮文字
     * @param positiveCallback 肯定回调
     * @param negativeText 否定按钮文字
     * @param negativeCallback 否定回调
     * @return
     */
    public AlertDialog alert(String title,String message,String positiveText,final Callback positiveCallback,String negativeText,final Callback negativeCallback){
        AlertDialog.Builder builder = new Builder(this.getActivity());
        builder.setTitle(title);
        builder.setMessage(message);
        if(positiveCallback==null && negativeCallback==null){
            builder.setPositiveButton("确认", new OnBtnClickListener(this.getActivity()) {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        }
        if(positiveCallback!=null){
            builder.setPositiveButton(StringUtils.isNotEmpty(positiveText)?positiveText:"确认", new OnBtnClickListener(this.getActivity()) {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    positiveCallback.callback(getActivity(), dialog, which);
                }
            });
        }
        if(negativeCallback!=null){
            builder.setNegativeButton(StringUtils.isNotEmpty(negativeText)?negativeText:"取消", new OnBtnClickListener(activity) {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    negativeCallback.callback(getActivity(), dialog, which);
                }
            });
        }
        AlertDialog alert = builder.create();
        alert.show();
        return alert;
    }

    /**
     * 弹出提示
     * @param title 标题
     * @param message 信息
     * @param positiveText 肯定按钮文字
     * @param positiveCallback 肯定回调
     * @param negativeText 否定按钮文字
     * @param negativeCallback 否定回调
     * @return
     */
    public AlertDialog alert(String title,String message,String positiveText,final Callback positiveCallback,String neutralText,final Callback neutralCallback,String negativeText,final Callback negativeCallback){
        AlertDialog.Builder builder = new Builder(this.getActivity());
        builder.setTitle(title);
        builder.setMessage(message);
        if(positiveCallback==null && negativeCallback==null){
            builder.setPositiveButton("确认", new OnBtnClickListener(this.getActivity()) {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        }
        if(positiveCallback!=null){
            builder.setPositiveButton(StringUtils.isNotEmpty(positiveText)?positiveText:"确认", new OnBtnClickListener(this.getActivity()) {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    positiveCallback.callback(getActivity(), dialog, which);
                }
            });
        }
        if(neutralCallback!=null){
            builder.setNeutralButton(StringUtils.isNotEmpty(neutralText)?neutralText:"不确定", new OnBtnClickListener(this.getActivity()) {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    neutralCallback.callback(getActivity(), dialog, which);
                }
            });
        }
        if(negativeCallback!=null){
            builder.setNegativeButton(StringUtils.isNotEmpty(negativeText)?negativeText:"取消", new OnBtnClickListener(activity) {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    negativeCallback.callback(getActivity(), dialog, which);
                }
            });
        }
        AlertDialog alert = builder.create();
        alert.show();
        return alert;
    }

    public AlertDialog alert(String title,String message){
        return alert(title, message, null, null, null, null);
    }

    public AlertDialog alert(String title,String message,String positiveText,Callback positiveCallback){
        return alert(title, message, positiveText, positiveCallback, null, null);
    }

    public abstract class OnBtnClickListener implements OnClickListener{
        private Activity activity;
        public OnBtnClickListener(Activity activity){
            this.setActivity(activity);
        }

        public Activity getActivity() {
            return activity;
        }

        public void setActivity(Activity activity) {
            this.activity = activity;
        }

        @Override
        public abstract void onClick(DialogInterface dialog, int which);

    }

    public interface Callback{
        public abstract void callback(Context context,DialogInterface dialog,int which);
    }

}